<?php
namespace App\Repository\Db;

use App\Repository\Contracts\SettingInterface;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-01-16 15:01
 *
 * @Description:
 */
class SettingRepository extends Repository implements SettingInterface
{
    protected $rules = [
        'created' => [
            'name' => 'required|min:2|max:32|unique:setting,name',
            'config' => 'required|min:2',
        ],
        'updated' => [
            'name' => 'required|min:2|max:32|unique:setting',
            'config' => 'required|min:2',
        ]
    ];

    protected $messages = [
        'name.required' => "配置名不能为空",
        'name.min' => "配置名不能少于2个字",
        'name.max' => "配置名不能大于32个字",
        'name.unique' => "配置名不能重复",
        'config.required' => "配置描述不能为空",
        'config.min' => "配置描述不能少于2个汉字"
    ];

    protected function modelName()
    {
        return 'App\Model\Setting';
    }

    public function lists()
    {
        return $this->all();
    }

    public function detail($id)
    {
        return $this->find($id);
    }

    public function findByName($name)
    {
        return $this->model->where('name', '=', $name)->first();
    }

    public function created(array $data)
    {
        $this->validate($data, 'created');

        return $this->save($data);
    }

    public function updated($id, array $data)
    {
        $this->validate($data, 'updated', ['name' => $id]);

        return $this->update($data, $id, 'id');
    }
}